from django.contrib.auth.management.commands import createsuperuser
from django.core.management import CommandError

class Command(createsuperuser.Command):
    def handle(self, *args, **options):
        try:
            super().handle(*args, **options)
            # 获取最新创建的用户
            user = self.UserModel.objects.latest('id')
            # 设置为超级用户
            user.is_superuser = True
            user.save()
            self.stdout.write(self.style.SUCCESS('Superuser created successfully.'))
        except Exception as e:
            raise CommandError(f'Error creating superuser: {str(e)}') 